package com.tjakus.arduino.bean;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.faces.event.ValueChangeEvent;

import com.tjakus.arduino.hibernate.HibernateUtil;
import com.tjakus.arduino.hibernate.Sensors;
import com.tjakus.arduino.hibernate.Statistics;
import com.tjakus.arduino.logging.Logging;


public class StatisticsBean {

	private List<Statistics> statisticsListPerSensor;
	private String selectedSensor;
	private List<String> sensorsList = this.init();
	
	
	public List<String> init() {
		Logging.getLogger().info("StatisticsBean - init [ ]");
		List<String> initSensorList = new ArrayList<String>();
		List<Sensors> usersList = HibernateUtil.getAllSensors();
		for (Iterator<Sensors> iter = usersList.iterator(); iter
				.hasNext();) {
			initSensorList.add(iter.next().getSensorId());
		}
		return initSensorList;
	}
	
	public List<Statistics> getStatisticsListPerSensor() {
		return statisticsListPerSensor;
	}

	public void setStatisticsListPerSensor(List<Statistics> newStatisticsListPerSensor) {
		this.statisticsListPerSensor = newStatisticsListPerSensor;
	}

	public String getSelectedSensor() {
		return selectedSensor;
	}

	public void setSelectedSensor(String newSelectedSensor) {
		this.selectedSensor = newSelectedSensor;
	}

	public List<String> getSensorsList() {
		return sensorsList;
	}

	public void setSensorsList(List<String> newSensorsList) {
		this.sensorsList = newSensorsList;
	}	

	public void sensorChanged(ValueChangeEvent event) {
		Logging.getLogger().info("StatisticsBean - sensorChanged [ " + event.getNewValue().toString() + " ]");
		selectedSensor = (String) event.getNewValue();
		statisticsListPerSensor = HibernateUtil.getStatisticsBySensorId(selectedSensor);
	}
	
}
